#!/bin/sh
# 
# SrvrPowerCtrl plugin helper script for Redhat, Fedora or other distros using rpm and yum.
#
# This script will update SqueezeCenter using a file downloaded by SrvrPowerCtrl
# from latest nighly releases at # http://downloads.slimdevices.com/nightly/latest/N.N/
#
# Possible args are:
#
# case 1:
#
#     $1 == /path/to/downloaded/update_file.rpm (e.g. /tmp/squeezecenter-7.2.1-0.1.23141.noarch.rpm)
#     $2 == hh:mm (e.g. 14:09)
#
# case 2:
#
#     $1 == hh:mm (e.g. 14:09 only)
#
# If case 1, use a package manager (i.e. rpm) to update squeezecenter
#
# If case 2, use a update manager (i.e. yum) to download & update squeezecenter
#
#
#

 
# Set the ext variable to the extension of the filetype we can handle:
ext=rpm

/bin/echo "$0 - scupdate.log" >/tmp/scupdate.log
/bin/date >>/tmp/scupdate.log


/bin/echo "Checking SqueezeCenter service status.." >>/tmp/scupdate.log

# Check the service status..stop it if it's running..
szSCStatus=`/etc/rc.d/init.d/squeezecenter status`
if (( $(/bin/echo $szSCStatus | /bin/egrep -c "running") >= 1 ))
  then
   /etc/rc.d/init.d/squeezecenter stop >>/tmp/scupdate.log
  else
   /bin/echo "SqueezeCenter is stopped.." >>/tmp/scupdate.log
fi

#check our args...if there is a path to a rpm, install it
#..otherwise, assume we want to update via yum...

#Check the args: SrvrPowerCtl should pass the downloaded filename and a hh:mm time as args..
if ([ $# -lt 2 ])
then
   goto use_updt_mgr
fi

echo "Checking download file $1.." >>/tmp/scupdate.log

#Was the right file downloaded? Check the extension..
szExt=`expr match "$1" '.*\([.]"$ext"\)'` 
if ([ "$szExt" != ".$ext" ])
  then
   echo "File $1 is not a .$ext file." >>/tmp/scupdate.log
   echo "Perhaps $0 is the wrong update script for your SC installation or OS distro.  Exiting." >>/tmp/scupdate.log
   /bin/echo "Restarting SqueezeCenter service.." >>/tmp/scupdate.log
   /etc/rc.d/init.d/squeezecenter start
   sleep 5
   exit -1
fi

#Do we have read access to the rpm file?
if ([ ! -r $1 ])
then+
   echo "Cannot read file $1.  Exiting." >>/tmp/scupdate.log
   /bin/echo "Restarting SqueezeCenter service.." >>/tmp/scupdate.log
   /etc/rc.d/init.d/squeezecenter start
   sleep 5
   exit -1
fi

/bin/echo "SC nightly release file downloaded by SrvrPowerCtrl: $1" >>/tmp/scupdate.log

#check to see if rpm thinks squeezecenter is installed
szSCStatus=`/bin/rpm -q squeezecenter`
if (( $(/bin/echo $szSCStatus | /bin/egrep -c "squeezecenter") < 1 ))
  then
   /bin/echo "rpm claims $szSCStatus." >>/tmp/scupdate.log
   /bin/echo "Perhaps $0 is the wrong update script for your SC installation or OS distro.  Exiting." >>/tmp/scupdate.log
   /bin/echo "Restarting SqueezeCenter service.." >>/tmp/scupdate.log
   /etc/rc.d/init.d/squeezecenter start
   sleep 5
   exit -1
fi

#Finally!  Let's update SC..
#Check to see if rpm likes the package.
/bin/echo "Testing package $1.." >>/tmp/scupdate.log
szSCStatus=`/bin/rpm --test -Uvh $1`
#Possible answers:
#Preparing... ##################################################
# - or - 
#package squeezecenter-7.2-0.1.23117.noarch is already installed 
if (( $(/bin/echo $szSCStatus | /bin/egrep -c "Preparing...") >= 1 ))
  then
    /bin/echo "Updating SqueezeCenter.." >>/tmp/scupdate.log
    szSCStatus=`/bin/rpm -Fvh $1`
  else
    /bin/echo "Update Failed.." >>/tmp/scupdate.log
fi

/bin/echo "Update results:" >>/tmp/scupdate.log
/bin/echo "$szSCStatus" >>/tmp/scupdate.log
/bin/echo "Update attempt completed.." >>/tmp/scupdate.log
/bin/echo "Removing $1.." >>/tmp/scupdate.log
/bin/rm "$1" >>/tmp/scupdate.log

#because rpm may have restarted the squeezecenter service..
/bin/echo "Checking SqueezeCenter service status.." >>/tmp/scupdate.log
szSCStatus=`/etc/rc.d/init.d/squeezecenter status`
#Did rpm restart the service?
if (( $(echo $szSCStatus | egrep -c "running") < 1 ))
   /bin/echo "Restarting SqueezeCenter service.." >>/tmp/scupdate.log
   /etc/rc.d/init.d/squeezecenter start
fi

bail_out:
echo 'Done!' >>/tmp/scupdate.log
exit 0

#OK, it looks like we want yum to handle the update..
use_updt_mgr:

# check for installed via yum:
/bin/echo "Checking to see if SqueezeCenter is installed via yum.." >>/tmp/scupdate.log
szSCStatus=`/usr/bin/yum info squeezecenter --disablerepo=*`

#Did rpm restart the service?
if (( $(echo $szSCStatus | egrep -c "running") >= 1 ))
  then
   /bin/echo "Update complete!" >>tmp/scupdate.log
  else
   /bin/echo "Restarting SqueezeCenter service.." >>/tmp/scupdate.log
   /etc/rc.d/init.d/squeezecenter start
fi

echo 'Done!' >>/tmp/scupdate.log
exit 0
